//
//  MyViewController.m
//  口袋
//
//  Created by dllo on 15/5/21.
//  Copyright (c) 2015年 口袋. All rights reserved.
//

#import "MyViewController.h"
#import "NewsCell.h"
#import "NewsPicCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <MJRefresh/MJRefresh.h>
#import "CollectDetailViewController.h"
#import "CollectDetailViewController.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface MyViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView * table;

@property (nonatomic, retain) NSMutableArray * array;

@end

@implementation MyViewController


- (void)dealloc
{
    [_table release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.navigationItem.title = @"我的收藏";
    }
    return self;
}

- (void)loadView
{
    [super loadView];
//    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
//    NSArray * arr = [defaults objectForKey:@"collectArray"];
//    self.array = [NSMutableArray arrayWithArray:arr];
   // [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:128/255.0 green:217/255.0 blue:215/255.0 alpha:1.0]];
    
    [self createTable];
}

- (void)createTable
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 49) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSArray * arr = [defaults objectForKey:@"collectArray"];
    self.array = [NSMutableArray arrayWithArray:arr];
    if (self.array.count == 0) {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您还没有任何收藏" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
    
    // 上拉加载
    [_table addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [_table.header beginRefreshing];
    
    [self.view addSubview:_table];
    [self.table release];
}

- (void)loadNewData
{
    [_table reloadData];
    [_table.header endRefreshing];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSArray * arr = [defaults objectForKey:@"collectArray"];
    self.array = [NSMutableArray arrayWithArray:arr];

    return self.array.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHT / 6.67;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CollectDetailViewController * collect = [[CollectDetailViewController alloc] init];
    NSDictionary * dic = [_array objectAtIndex:indexPath.row];
    collect.dic = dic;
    [self.navigationController pushViewController:collect animated:YES];
    [collect release];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 普通形式的cell
    
    NSDictionary * dic = [self.array objectAtIndex:indexPath.row];
    
    NewsCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[NewsCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    NSURL * url = [NSURL URLWithString:[dic objectForKey:@"imgsrc"]];
    [cell.imgsrc sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"MyCollect.png"]];
    cell.title.text = [dic objectForKey:@"title"];
    cell.digest.text = [dic objectForKey:@"digest"];
    
    // 跟帖数
    NSNumber * a = [dic objectForKey:@"replyCount"];
    NSInteger b = [a integerValue];
    if (b > 10000) {
        float c = b * 1.0 / 10000 * 1.0;
        cell.comments.text = [NSString stringWithFormat:@"%.1f 万跟帖", c];
    } else {
        cell.comments.text = [NSString stringWithFormat:@"%ld 跟帖", (long)b];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
